<?php
/**
 * QFSHOP 控制台首页
 * ============================================================================
 * 青峰网络科技有限公司，并保留所有权利。
*/
define('IN_ZYS', true);
require(dirname(__FILE__) . '/includes/init.php');
if($_REQUEST['act'] == 'ads_list'){
	//查询分类筛选
	$sql="select position_id,position_name from ".ly()."ad_position";
	$p_row=$db->getAll($sql);
	//选择查询改分类下的对应广告
	$position_id=isset($_GET['pid'])?$_GET['pid']:0;//获取分类位置id
    if($position_id==''){
        $position_id=$_REQUEST['position_id'];
    }
	if($position_id<1){
        $sql="select l.*,p.position_name from ".ly()."ad l,".ly()."ad_position p where l.position_id=p.position_id order by sort_id asc";
		$smarty->assign('pid','0');
    }else{
        $sql="select l.*,p.position_name from ".ly()."ad l,".ly()."ad_position p where l.position_id=p.position_id and p.position_id='{$position_id}' order by sort_id asc";
		$smarty->assign('pid',$position_id);
    }
    /*进入列表页*/
	$num='15';
	$res=$db->query($sql);
	$total_count=$db->num_rows($res);
	$pagelist = new page(array('total'=>$total_count, 'perpage'=>$num));
	$currenpage=$pagelist->nowindex;
	$start=($currenpage-1)*$num;
	if($total_count>$num){
		$smarty->assign('page',$pagelist->show(3));
	}else{
		$smarty->assign('page','');
	}
	$limit=" limit ".$start.','.$num;
	/*分页结束*/
    
	$sql.=$limit;
    $row=$db->getAll($sql);
    $smarty->assign('list',$row);
	$smarty->assign('p_list',$p_row);
    $smarty->display('ads_list.html');
	
}elseif($_REQUEST['act']=='listedit' || $_REQUEST['act']=='add'){
    // 进入修改列表广告页面
    $ad_id=$_GET['ad_id'];
    $sql="select * from ".ly()."ad where ad_id='{$ad_id}'";
    $row=$db->getRow($sql);
    $sql1="select position_id,position_name from ".ly()."ad_position order by position_id desc";
    $row1=$db->getAll($sql1);
    if($_POST){
        $ad_name=$_POST['ad_name'];
        $position_id=$_POST['position_id'];
        $start_time=strtotime($_POST['start_time']);
        $end_time=strtotime($_POST['end_time']);
        $ad_link=$_POST['ad_link'];
		$ad_code=$_POST['ad_code'];
		@$ad_imgs=$_FILES['ad_picture']['error'];
		if($ad_imgs==0){
			require("../includes/upload_admin.php");//加载图片上传类
			$up=new upphoto;
			$up->get_ph_tmpname($_FILES['ad_picture']['tmp_name']);
			$up->get_ph_type($_FILES['ad_picture']['type']);
			$up->get_ph_size($_FILES['ad_picture']['size']);
			$up->get_ph_name($_FILES['ad_picture']['name']);
			$up->save();
			$ad_code=$up->ph_name;
			
		}
        $enabled=$_POST['enabled'];
        $sort_id=$_POST['sort_id'];
        $link_h5=$_POST['link_h5'];
        $link_ios=$_POST['link_ios'];
        $link_android=$_POST['link_android'];
		if($_REQUEST['act']=='add'){
			$sql="insert into ".ly()."ad (ad_name,position_id,start_time,end_time,ad_link,ad_code,enabled,sort_id,link_h5,link_ios,link_android) values ('{$ad_name}','{$position_id}','{$start_time}','{$end_time}','{$ad_link}','{$ad_code}','{$enabled}','{$sort_id}','{$link_h5}','{$link_ios}','{$link_android}')";
		}else{
			$sql="update ".ly()."ad set ad_name='{$ad_name}',position_id='{$position_id}',start_time='{$start_time}',end_time='{$end_time}',ad_link='{$ad_link}',ad_code='{$ad_code}',enabled='{$enabled}',sort_id='{$sort_id}',link_h5='{$link_h5}',link_ios='{$link_ios}',link_android='{$link_android}' where ad_id='{$ad_id}'";
		}
        $db->query($sql);
        if(@mysql_affected_rows){
            $href[]=array('text'=>"返回广告列表",'href'=>'ads.php?act=ads_list');
            sys_msg("操作成功！",$href);
        }else{
            $href[]=array('text'=>"重新编辑",'href'=>'history.back()');
            sys_msg("操作失败！",$href,false);
        }
    }
    $smarty->assign('list',$row);
    $smarty->assign('position',$row1);
    $smarty->display('ads_listedit.html');
	
}elseif($_REQUEST['act']=='ads_position'){
    // 进入广告位置页面
    $sql="select * from ".ly()."ad_position order by position_id desc";
    $row=$db->getAll($sql);
    $smarty->assign('ad_posinfo',$row);
    $smarty->display('ads_position.html');
}elseif($_REQUEST['act']=='ads_posiedit' || $_REQUEST['act']=='ads_add'){
    // 进入广告位置修改页面
    if($_GET['position_id']){
        $position_id=$_GET['position_id'];
        $sql="select * from ".ly()."ad_position where position_id = '{$position_id}'";
        $row=$db->getRow($sql);
        $smarty->assign('position',$row);
    }
    // 修改页面提交数据
    if($_POST){
        $position_name=$_POST['position_name'];
        $ad_width=$_POST['ad_width'];
        $ad_height=$_POST['ad_height'];
        $position_desc=$_POST['position_desc'];
        $position_style=$_POST['position_style'];
        if($_REQUEST['act']=='ads_add'){
            $sql="insert into ".ly()."ad_position (position_name,ad_width,ad_height,position_desc,position_style) values('{$position_name}','{$ad_width}','{$ad_height}','{$position_desc}','{$position_style}')";
            $db->query($sql);
        }else{echo "<br>";
            $sql="update ".ly()."ad_position set position_name='{$position_name}',ad_width='{$ad_width}',ad_height='{$ad_height}',position_desc='{$position_desc}',position_style='{$position_style}' where position_id='{$position_id}'";
            $db->query($sql);
        }
        if(@mysql_affected_rows){
            $href[]=array('text'=>"返回广告列表",'href'=>'ads.php?act=ads_position');
            sys_msg("广告位置修改成功！",$href);
        }else{
            $href[]=array('text'=>"重新编辑",'href'=>'history.back()');
            sys_msg("广告位置修改失败！",$href);
        }
    }
    $smarty->display('ads_posiedit.html');
}elseif($_REQUEST['act']=='ads_del'){
    //删除广告位置
    if($_GET['position_id']){
        $position_id=$_GET['position_id'];
        $sql="delete from ".ly()."ad_position where position_id='{$position_id}'";
    }elseif($_GET['ad_id']){
        $ad_id=$_GET['ad_id'];
        $sql="delete from ".ly()."ad where ad_id='{$ad_id}'";
    }
    $db->query($sql);
    if(@mysql_affected_rows){
        if($_GET['position_id']){
            $href[]=array('text'=>"返回广告列表",'href'=>'ads.php?act=ads_position');
        }elseif($_GET['ad_id']){
            $href[]=array('text'=>"返回广告列表",'href'=>'ads.php?act=ads_list');
        }
        sys_msg("广告删除成功！",$href);
    }else{
        if($_GET['position_id']){
            $href[]=array('text'=>"返回广告列表",'href'=>'ads.php?act=ads_position');
        }elseif($_GET['ad_id']){
            $href[]=array('text'=>"返回广告列表",'href'=>'ads.php?act=ads_list');
        }
        sys_msg("广告删除失败！",$href);
    }
}

?>
